<template>
	<view class="content">
		<view class="text-center" style="font-size: 20px">
			{{group_name}}
		</view>

		<view v-if="!accessList.length" class="padding">当前还没有出入登记点</view>
		<view v-else class="margin-lr">
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action">
					<text class="cuIcon-circlefill text-olive"></text> 出入登记点：
				</view>
			</view>
			<view class="cu-list menu">
				<view class="cu-item flex"
					v-for="(accessListVal, accessListIdx) in accessList"
					:key="accessListIdx">
					<view class="flex">
						<text class="cuIcon-title text-olive"></text>
						<navigator :url="'../access/access_detail?access_id='+accessListVal._id">{{accessListVal.name || '未命名'}}</navigator>
					</view>
					<view class="flex">
						<switch class="margin-right" :class="accessList[accessListIdx].is_active?'checked':''" :checked="accessList[accessListIdx].is_active" @change="activeChange($event,accessListVal._id,accessListIdx)" />
						<button class="cu-btn" @click="deleteAccess(accessListVal._id,accessListIdx)">删除</button>
					</view>
				</view>
			</view>
		</view>

		<view class="padding">
			<navigator class="cu-btn block bg-blue margin-tb-sm lg" :url="'../access/create_access?group_id='+group_id">创建出入登记点</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				group_id: "",
				group_name: "",

				accessList: [],
			}
		},
		onLoad() {
			this.group_id = this.$route.query.group_id;

			if (this.group_id) {
				this.getAccessList();
			}
		},
		methods: {
			getAccessList() {
				let group_id = this.group_id;

				uniCloud.callFunction({
					name: "ha_get_access",
					data: {
						type: 0,
						group_id
					},
					success: ({result}) => {
						// console.log(result)
						this.accessList = result;
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			},
			activeChange(e,access_id,index) {
				// console.log(e.target.value);
				this.accessList[index].is_active = e.target.value;

				uniCloud.callFunction({
					name: "ha_update_access",
					data: {
						type: 0,
						access_id,
						is_active: e.target.value
					},
					success: ({result}) => {
						// console.log(result);
						if ( result.status === 0 ) {
							uni.showToast({
								title: `${result.msg || "更新成功！"}`,
								duration: 1000
							});
							// location.reload();
						} else {
							uni.showToast({
								icon: 'none',
								title: `${result.msg || "网络异常，请稍后重试！"}`,
								duration: 1000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			},
			deleteAccess(access_id,index) {
				uni.showModal({ content: '确认删除吗？', success: (res) => { if (res.confirm) {
					// console.log(access_id);
					let token = uni.getStorageSync("token");
					let group_id = this.group_id;

					if (token) {
						uniCloud.callFunction({
							name: "ha_del_access",
							data: {
								token,
								group_id,
								access_id
							},
							success: ({result}) => {
								// console.log(result);
								if ( result.status === 0 ) {
									uni.showToast({
										title: `${result.msg || "删除成功！"}`,
										duration: 1000
									});
									// location.reload();
									this.accessList.splice(index, 1);
								} else {
									uni.showModal({
										content: `${result.msg || "网络异常，请稍后重试！"}`,
										showCancel: false,
										complete: function () {
											uni.navigateTo({url: "/pages/user/user_login"});
										}
									});
								}
							},
							fail() {
								uni.showToast({
									icon: 'none',
									title: '网络异常，请稍后重试！'
								});
							}
						});
					} else {
						uni.navigateTo({url: "/pages/user/user_login"});
					}
				}}});
			}
		}
	}
</script>

<style>

</style>
